// pages/UserStatistics/UserDeatils/UserDeatils.js
const controllers = require('../../controllers/controllerIndex.js').controller;

const controller = require("../../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon1:'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210926/891687422373396480.png',
    icon2:'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210924/891010713655443456.png',
    icon3:'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210924/891010733087653888.png',
    icon4:'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/o2o/agent/table/agent%402x.png',
    noSelectIcon:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210810/874665097627172864.png",
    selectIcon:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210810/874665118292508672.png",
    array:[
      {
        name:"普通用户",
        id:0
      },
      {
        name:"普通会员",
        id:2
      },
      {
        name:"VIP会员",
        id:1
      },
      {
        name:"共享股东",
        id:3
      },
      {
        name:"代理商",
        id:4
      },
      
    ],
    outPay:"",
    value:{
      shopId:"",
      remarks:"",
      sex:"",
      birthday:"",
      desc:"",
      accountRelationId:"",
      dispenserDesc:""
    },
    sexList:['男','女'],
    isSave:true,
    friendNum:0,
    friendList:[],
    super:"",
    superId:"",
    accountRelationId:"",
    descHide:0
  },

  // 切换是否展示会员
  changeDescHide(){
    if(this.data.descHide==1){
      this.setData({
        descHide:0
      })
    }else{
      this.setData({
        descHide:1
      })
    }
  },

  //跳转优惠券列表
  toCouponList(){
    wx.navigateTo({
      url: '/pages/userStatistics/UserDeatils/couponList?accountId='+this.data.accountId,
    })
  },

  //跳转会员卡列表
  toVipCardList(){
    wx.navigateTo({
      url: '/pages/userStatistics/UserDeatils/vipCardList?accountId='+this.data.accountId,
    })
  },

  //跳转会员金币
  toVipBean(e){
    let id=e.currentTarget.dataset.id
    let shopRelId=e.currentTarget.dataset.shoprelid
    wx.navigateTo({
      url: '/pages/userStatistics/UserDeatils/vipBean?id='+id+"&shopRelId="+shopRelId,
    })
  },

  //跳转好友列表
  toFriendList(){
    wx.navigateTo({
      url: '/pages/userStatistics/UserDeatils/friendList?accountId='+this.data.accountId,
    })
  },

  //获取用户的好友列表
  getFriendList(){
    let value={}
    value.accountId=this.data.accountId
    value.shopId=wx.getStorageSync('shopId')
    controller.accountFriends(value).then(res=>{
      let list=res.data.friendList
      let friendList=[]
      /*
      if(res.data.super){
        res.data.super.mobile= res.data.super.mobile.substr(0,3)+"****"+res.data.super.mobile.substr(7)
      }*/
      this.setData({
        friendNum:res.data.friendList.length,
        super:res.data.super,
        superId:res.data.superId
      })
      if(res.data.friendList.length<=3){
        this.setData({
          friendList:list
        })
      }else{
        for(let i=0;i<3;i++){
          friendList.push(list[i])
        }
        this.setData({
          friendList:friendList
        })
      }
    })
  },

  //解绑上级
  unbind(){
    let that=this
    wx.showModal({
      title:"提示",
      content: '确定要解绑吗？',
      success (res) {
        if (res.confirm) {
          let value={}
          value.accountId=that.data.accountId
          value.shopId=wx.getStorageSync('shopId')
          controller.unbind(value).then(res=>{
            wx.showToast({
              title: '操作成功',
            })
            that.getFriendList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  changeDate(e){
    this.setData({
      'value.birthday':e.detail.value
    })
  },

  //跳转积分详情
  toIntegral(){
    let accountId=this.data.detail.accountId
    wx.navigateTo({
      url: '/pages/userStatistics/WriteOff/integralDetail/integralDetail?userId='+accountId,
    })
  },

  //跳转会员账户详情
  toAccountDetail(){
    let accountId=this.data.detail.accountId
    wx.navigateTo({
      url: '/pages/userStatistics/UserDeatils/accountDetail?userId='+accountId+"&accountRelationId="+this.data.accountRelationId,
    })
  },

  //切换性别
  changeSex(e){
    this.setData({
      'value.sex':this.data.sexList[e.detail.value]
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this
    this.setData({
      'value.shopId':wx.getStorageSync('shopId'),
      accountId:options.accountId,
      accountRelationId:options.id
    })
    let params={
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
      // accountRelationId:options.id,
      accountId:options.accountId
    }
    this.setData({
      params:params
    })
    controllers.accountDetail(
      params
    ).then(res =>{
      let vipList=[]
        if(res.data.vipList&&res.data.vipList.length>0){
          res.data.vipList.forEach(element => {
            let p =0
            if(element.endTime){
            let endTime =that.getDateTime(element.endTime)
            let createTime=that.getDateTime(element.createTime)
           let total= endTime-createTime
           let time = endTime-new Date().getTime(); 
             p =time/total;
           }
           element.p=p*100
           vipList.push(element)
          });
        }

        // res.data.accountMobile=res.data.accountMobile.slice(0,3)+"****"+res.data.accountMobile.slice(7)
    
      that.setData({
        detail:res.data,
        outPay:(res.data.incomePrice-res.data.balance).toFixed(2),
        vipList,
        "value.accountRelationId":res.data.id||'',
        "value.remarks":res.data.remarks||'',
        "value.sex":res.data.sex||"",
        "value.birthday":res.data.birthday||'',
        "value.desc":res.data.desc||'',
        "value.dispenserDesc":res.data.dispenserDesc||'',
        vip:res.data.vip,
        descHide:res.data.descHide
      })
      if(res.data.remarks){
        this.setData({
          isSave:false
        })
      }else{
        this.setData({
          isSave:true
        })
      }
    })
  },
  //获取输入用户数据
  getValue(e){
    let type=e.currentTarget.dataset.type
    let value=e.detail.value
    console.log(e)
    if(type=="remarks"){
      this.setData({
        'value.remarks':value
      })
    }else if(type=="sex"){
      this.setData({
        'value.sex':value
      })
    }else if(type=="birthday"){
      this.setData({
        'value.birthday':value
      })
    }else if(type=="desc"){
      this.setData({
        'value.desc':value
      })
    }else if(type=="dispenserDesc"){
      this.setData({
        'value.dispenserDesc':value
      })
    }
  },

  //跳转线下开单
  toCreateOrder(){
    let accountRelationId=this.data.detail.id
    let accountNick=this.data.detail.accountNick
    let accountMobile=this.data.detail.accountMobile
    wx.navigateTo({
      url: '/pages/settlement/Offline/Offline?accountRelationId='+accountRelationId+"&accountNick="+accountNick+"&accountMobile="+accountMobile+"&vip="+this.data.vip,
    })
  },

  //跳转项目核销
  WriteOff(){
    let accountId=this.data.detail.accountId
    wx.navigateTo({
      url: '/pages/userStatistics/WriteOff/WriteOff?accountId='+accountId,
    })
  },

  //跳转线下开卡
  toCreateCard(){
    let accountId=this.data.detail.accountId
    let accountNick=this.data.detail.accountNick
    let accountMobile=this.data.detail.accountMobile
    let vip=this.data.detail.vip
    let balance=this.data.detail.balance
    wx.navigateTo({
      url: '/pages/goodsManagement/OfflineSetVip/OfflineSetVip?accountId='+accountId+"&accountNick="+accountNick+"&accountMobile="+accountMobile+"&vip="+vip+"&balance="+balance,
    })
  },

  //跳转查看订单页
  toWatchOrder(){
    let accountId=this.data.detail.accountId
    let accountNick=this.data.detail.accountNick
    let accountMobile=this.data.detail.accountMobile
    wx.navigateTo({
      url: '/pages/order/orderTypeList/orderTypeList?accountId='+accountId+"&accountNick="+accountNick+"&accountMobile="+accountMobile,
    })
  },

  //备注用户信息
  subPersonMsg(e){
      let value=this.data.value
      value.descHide=this.data.descHide
      controllers.setAccountDesc(this.data.value).then(res=>{
        wx.showToast({
          icon:"none",
          title: '备注成功',
          duration:1500
        })
      })
    // }
  },

  getDateTime(date){
   
    date= date.replace(new RegExp("-","gm"),"/");
    var time = (new Date(date)).getTime(); 
    return time
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getFriendList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }


  //选择会员等级
  bindPickerChange(e){
    let that=this
    let value={}
    let params=this.data.params
    value.vip=this.data.array[e.detail.value].id
    value.shopId=wx.getStorageSync('shopId') 
    value.accountId=this.data.detail.accountId
    controllers.updataVip(value).then(res=>{
      wx.showToast({
        icon:"none",
        title:"修改成功",
        duration:1500
      })
      controllers.accountDetail(
        params
      ).then(res =>{
        let vipList=[]
          if(res.data.vipList&&res.data.vipList.length>0){
            res.data.vipList.forEach(element => {
              let p =0
              if(element.endTime){
              let endTime =that.getDateTime(element.endTime)
              let createTime=that.getDateTime(element.createTime)
             let total= endTime-createTime
             let time = endTime-new Date().getTime(); 
               p =time/total;
             }
             element.p=p*100
             vipList.push(element)
            });
          }
      
        that.setData({
          detail:res.data,
          vipList
        })
      })
    })

  }
})